//
//  EFOrderProductCell.m
//  EnjogFood
//
//  Created by 王芳 on 17/5/13.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFOrderProductCell.h"
#import "EFOrderProductItem.h"
#import "EFReturnProductItem.h"

@interface EFOrderProductCell ()
@property (strong, nonatomic) UIImageView *productImageV;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *unitLabel;
@property (strong, nonatomic) UILabel *countLabel;
@property (strong, nonatomic) UILabel *priceLabel;

@property (strong, nonatomic) UIView *line;
@end

@implementation EFOrderProductCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

- (void)initUI{
    [self.contentView addSubview:self.productImageV];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.unitLabel];
    [self.contentView addSubview:self.countLabel];
    [self.contentView addSubview:self.priceLabel];
     [self.contentView addSubview:self.line];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.line.left = 20;
    self.line.bottom = self.height;
}

- (void)setObject:(EFOrderProductItem *)item{
    [self.productImageV setImageWithURL:[NSURL URLWithString:item.img]];
    self.productImageV.left = 20;
    self.productImageV.top = 14;
    
    self.titleLabel.text = item.product_name;
    [self.titleLabel sizeToFit];
    self.titleLabel.width = kScreenWidth - 20 - self.productImageV.right - 15;
    self.titleLabel.left = self.productImageV.right + 15;
    self.titleLabel.top = self.productImageV.top;
    
    self.unitLabel.text = item.model_name;
    [self.unitLabel sizeToFit];
    self.unitLabel.left = self.titleLabel.left;
    self.unitLabel.top = self.titleLabel.bottom + 5;
    
    self.countLabel.text = [NSString stringWithFormat:@"x %@",item.qty];
    [self.countLabel sizeToFit];
    self.countLabel.left = self.titleLabel.left;
    self.countLabel.top = self.unitLabel.bottom;
    
    self.priceLabel.text = [NSString stringWithFormat:@"￥%@",item.price];
    [self.priceLabel sizeToFit];
    self.priceLabel.left = self.titleLabel.left;
    self.priceLabel.top = self.countLabel.bottom + 10;
}

- (void)setReturnObject:(EFReturnProductItem *)item{
    [self.productImageV setImageWithURL:[NSURL URLWithString:item.img]];
    self.productImageV.left = 20;
    self.productImageV.top = 14;
    
    self.titleLabel.text = item.productName;
    [self.titleLabel sizeToFit];
    self.titleLabel.width = kScreenWidth - 20 - self.productImageV.right - 15;
    self.titleLabel.left = self.productImageV.right + 15;
    self.titleLabel.top = self.productImageV.top;
    
    self.unitLabel.text = item.modelName;
    [self.unitLabel sizeToFit];
    self.unitLabel.left = self.titleLabel.left;
    self.unitLabel.top = self.titleLabel.bottom + 5;
    
    self.countLabel.text = [NSString stringWithFormat:@"x %@",item.qty];
    [self.countLabel sizeToFit];
    self.countLabel.left = self.titleLabel.left;
    self.countLabel.top = self.unitLabel.bottom;
    
    self.priceLabel.text = [NSString stringWithFormat:@"￥%@",item.price];
    [self.priceLabel sizeToFit];
    self.priceLabel.left = self.titleLabel.left;
    self.priceLabel.top = self.countLabel.bottom + 10;

}

- (UIImageView *)productImageV{
    if (!_productImageV) {
        _productImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
        _productImageV.backgroundColor = RGBCOLOR(0xf3f3f3);
        _productImageV.contentMode = UIViewContentModeScaleAspectFill;
        _productImageV.clipsToBounds = YES;
    }
    return _productImageV;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:13];
        _titleLabel.textColor = kBlack333;
    }
    return _titleLabel;
}

- (UILabel *)unitLabel{
    if (!_unitLabel) {
        _unitLabel = [[UILabel alloc] init];
        _unitLabel.font = [UIFont systemFontOfSize:13];
        _unitLabel.textColor = kBlack999;
    }
    return _unitLabel;
}

- (UILabel *)countLabel{
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] init];
        _countLabel.font = [UIFont systemFontOfSize:13];
        _countLabel.textColor = kBlack999;
    }
    return _countLabel;
}

- (UILabel *)priceLabel{
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.font = [UIFont systemFontOfSize:10];
        _priceLabel.textColor = RGBCOLOR(0x69ba45);
    }
    return _priceLabel;
}

- (UIView *)line{
    if (!_line) {
        _line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 2*20, 1)];
        _line.backgroundColor = RGBCOLOR(0xececec);
    }
    return _line;
}

@end
